#include <QCoreApplication>
#include <QDebug>
#include <ctime>
#include <QtCore/qmath.h>
long Pow(long x,long n);
long Pow_recur(long x,long n);
long Pow_bin(long x,long n);
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    long x,y,n;
    x=7,n=10;//Case;
    double start=(double) clock();
    y=qPow(x,n);
    double end1=(double) clock();
    qDebug()<<y<<"\n";
    qDebug()<<end1-start<<"ms\n";
    y=Pow(x,n);
    double end2=(double) clock();
    qDebug()<<y<<"\n";
    qDebug()<<end2-end1<<"ms\n";
    y=Pow_recur(x,n);
    double end3=(double) clock();
    qDebug()<<y<<"\n";
    qDebug()<<end3-end2<<"ms\n";
    y=Pow_bin(x,n);
    double end4=(double) clock();
    qDebug()<<y<<"\n";
    qDebug()<<end4-end3<<"ms\n";
    return a.exec();
}
long Pow(long x,long n)
{
    long y=1;
    for (int i = 0; i < n; i++)
    {
        y=y*x;
    }
    return y;
}
long Pow_recur(long x,long n)
{
    if (n == 0)
    return 1;
    else if (n % 2 == 1)
        return Pow_recur(x, n - 1) * x ;
    else
    {
        long temp = Pow_recur(x, n / 2) ;
        return temp * temp ;
    }
}
long Pow_bin(long x,long n)
{
    int ans = 1;
    while(n){
        if(n&1)        //如果n的当前末位为1
            ans *= x;  //ans乘上当前的a
        x *= x;        //a自乘
        n >>= 1;       //n往右移一位
    }
    return ans;
}
